const app = new getApp();
var time = require("../../../../utils/util.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    prompt: '',
    vg_id: '',
    qrcode: '',
    info: '',
    virtual_detail: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    let vg_id = options.vg_id;

    that.setData({
      vg_id: vg_id,
    })
    that.loadVirtualInfo();
    if (vg_id > 0) {
      that.loadVerificationList();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  loadVirtualInfo: function() {
    let that = this;
    let vg_id = that.data.vg_id;

    app.sendRequest({
      url: 'System.Order.getWapVirtualGoodsShare',
      data: {
        vg_id: vg_id
      },
      success: function(res) {
        let code = res.code;
        let data = res.data;

        if (code == 0) {
          let info = app.checkEmpty(res.data, '');

          info.time = '不限制';
          info.end_time = app.checkEmpty(info.end_time, 0);
          if (info.end_time != 0) {
            info.time = info.end_time == 0 ? info.time : '到' + time.formatTime(info.end_time, 'Y-M-D') + '之前有效';
          }
          let qrcode = app.checkEmpty(info.path, '');
          if (qrcode != '' && qrcode != -1) {
            qrcode = app.IMG(qrcode);
          } else if (qrcode == -1) {
            app.showBox(that, '二维码生成失败，请检查该二维码指向页面是否在小程序线上版本中存在', 3000);
          }
          console.log(res);
          that.setData({
            info: info,
            qrcode: qrcode
          })
        }
      }
    })
  },

  loadVerificationList: function() {
    let that = this;
    let vg_id = that.data.vg_id;
    app.sendRequest({
      url: 'System.Order.virtualGoodsVerificationList',
      data: {
        vg_id: vg_id
      },
      success: function(res) {

        if (res.code == 0) {
          let virtual_detail = app.checkEmpty(res.data, []);

          for (let index in virtual_detail) {
            let times = virtual_detail[index].create_time;
            virtual_detail[index].create_time = time.formatTime(times, 'Y-M-D h:m:s');
          }
          that.setData({
            virtual_detail: virtual_detail
          })
        }
      }
    })
  },
})